package cn.xkai.exercise.c;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 四数相加 II
 * @author: kaixiang
 * @date: 2022-07-28
 **/
public class Solution106 {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int n1 : nums1) {
            for (int n2 : nums2) {
                int key = n1 + n2;
                int count = map.getOrDefault(key, 0) + 1;
                map.put(key, count);
            }
        }

        int res = 0;
        for (int n3 : nums3) {
            for (int n4 : nums4) {
                int key = -n3 - n4;
                if (map.containsKey(key)) {
                    res += map.get(key);
                }
            }
        }
        return res;
    }
}
